package com.eascs.dubbo.loan.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.common.service.IO2oLoanapplyDubboService;
import com.eascs.admin.common.vo.O2oLoanapplyInfo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;

@Service
public class O2oLoanapplyDubboServiceImpl extends BaseO2oDao  implements IO2oLoanapplyDubboService {
	
	@Override
	public PageResult<O2oLoanapplyInfo> getLoanapplyInfo(int pageSize,int pageIndex) {
		if(pageSize == 0)
			pageSize = 50;
		SqlPageQuery<O2oLoanapplyInfo> sqlPageQuery = new SqlPageQuery<>(O2oLoanapplyInfo.class, pageSize, pageIndex);
		StringBuffer querySql = new StringBuffer();
//		querySql.append("SELECT DISTINCT L.LOANAPPLY_NO AS LOANAPPLYNO,L.CREATE_DATE AS CREATEDATE , L.BILL_CODE AS BILLCODE,L.OPERATOR_NO AS OPERATORNO,L.OPERATOR_NAME AS OPERATORNAME,L.LTD AS LTD,L.LTD_NAME AS LTDNAME,	P.PROVINCE_CODE AS PROVINCECODE,P.PROVINCE_NAME AS PROVINCENAME,L.WF_TYPE AS WFTYPE ");
//		querySql.append(" FROM	O2O_LOANAPPLY L LEFT JOIN	O2O_PROVINCE_PLATFORM P ON	P.LTD = L.LTD WHERE 1=1 ");
		
		querySql.append("SELECT DISTINCT L.LOANAPPLY_NO AS LOANAPPLYNO,L.CREATE_DATE AS CREATEDATE , L.BILL_CODE AS BILLCODE,L.OPERATOR_NO AS OPERATORNO,L.OPERATOR_NAME AS OPERATORNAME, L.LTD AS LTD,L.LTD_NAME AS LTDNAME,	P.province_code AS PROVINCECODE,P.province_name AS PROVINCENAME,L.WF_TYPE AS WFTYPE ");
		querySql.append("FROM	O2O_LOANAPPLY L LEFT JOIN	o2o_platform_regional P ON	P.platform_id = L.LTD WHERE 1=1 ");
		sqlPageQuery.setQuerySql(querySql.toString());
		return this.findDataWithPage(sqlPageQuery);
		
		
	}

	@Override
	public PageResult<O2oLoanapplyInfo> getAgingExportLoanInfo(int pageSize, int pageIndex) {
		if(pageSize == 0)
			pageSize = 50;
		SqlPageQuery<O2oLoanapplyInfo> sqlPageQuery = new SqlPageQuery<>(O2oLoanapplyInfo.class, pageSize, pageIndex);
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT DISTINCT L.LOANAPPLY_NO AS loanapplyNo, L.BILL_CODE AS billCode,L.OPERATOR_NO AS operatorNo,L.OPERATOR_NAME AS operatorName,L.LTD_NAME AS ltdName,P.province_code AS provinceCode,P.province_name AS provinceName,u.user_full_name AS customeRname , L.loan_type AS loanType, L.create_date AS createDate, L.wf_type AS wfType");
		querySql.append(" FROM	O2O_LOANAPPLY L LEFT JOIN	o2o_platform_regional P ON	P.platform_id = L.LTD ");
		querySql.append(" LEFT JOIN o2o_loan_user u ON u.loan_id = l.loan_id");
		querySql.append(" WHERE 1=1 ");
		sqlPageQuery.setQuerySql(querySql.toString());
		return this.findDataWithPage(sqlPageQuery);
	}

	@Override
	public PageResult<Object[]> getLoanapplyInfoByIdCard(String idCard) {
		// TODO Auto-generated method stub
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT y.loanapply_no,y.bill_code,u.user_full_name,y.operator_name,y.ltd_name,l.regional_name,d.loan_date ");  
		sql.append(" from o2o_loanapply y  ");
		sql.append(" LEFT JOIN o2o_loan_user u on u.loanapply_no=y.loanapply_no and u.spouse_id is NULL and sponsor_type is NULL ");
		sql.append(" LEFT JOIN o2o_platform_regional l on y.ltd=l.platform_id");
		sql.append(" LEFT JOIN o2o_bank_loan_data d on y.bill_code=d.bill_code");
		sql.append(" WHERE y.loan_flg='Y' and u.document_no = ?");
		sqlPageQuery.addParam(idCard);
		sqlPageQuery.setQuerySql(sql.toString());
		return this.findDataWithPage(sqlPageQuery);
	}

	@Override
	public Integer isSpouseExsitNotHK(String loanUserId,String loanapply_no) {
		String sql="SELECT count(1) FROM o2o_loan_user r LEFT JOIN o2o_loanapply ly ON r.loanapply_no = ly.loanapply_no WHERE r.document_no IN ( SELECT document_no FROM o2o_loan_user u WHERE u.loanapply_no = ? AND loan_id = ? ) AND wf_type <> 'HN' AND wf_type <> 'T' AND ly.loanapply_no <> ?" ;
		return this.findInteger(sql, loanapply_no,loanUserId,loanapply_no);
	}

}
